<?php


/*   Description  of this file
*    Author king-of-war
*    Last Modified 2009-4-8
*    This class designed  for the count operation
*/
class count {
	private $url;
	/*
	 * load the rootpath
	 */
	function __construct($url) {
		$this->set_url($url);
		/*
		 * init the conn class
		 */
		include_once $url . 'includes/conn.php';
		include_once $url . 'admin/class/error.class.php';
	}

	/*
	 * define count add function
	 *
	 * add count with flush
	 */
	function count_add($ip, $page) {
		$error = new Errmsg();

		if (isset ($ip)) {
			/*
			 * check ip with time
			 */

			$conn = new conn();

			$sql = sprintf("INSERT INTO count (id,page,ip, time) VALUES (NULL, '%s','%s', NOW());", $page, $ip);

			$conn->set_sql($sql);

			$line = $conn->get_line();

		} else {
			$info = '无效IP';
			$error->error_chinese($info);
			$line = -1;
		}

		return $line;
	}

	/*
	 *define count read function
	 *
	 *ids
	 */

	function count_read($ids) {
		$url = $this->get_url();
		include_once $url . 'includes/entity/count.entity.php';

		if (is_array($ids)) {
			/* get array code */
			$a = $ids[0];
			$b = $ids[1];
			$sql = sprintf("select * from count ORDER BY id DESC  limit %d,%d ", $a, $b);

		} elseif(empty($id)){
			$sql = "select * from count ORDER BY ip ASC ";
		}

		else {
			/* get one code */
			$sql = sprintf("select * from count where id='%d'", $ids);

		}

		/*
		 * common part for read
		 */
		$conn = new conn();

		$conn->set_sql($sql);

		$line = $conn->get_line();

		if ($line) {
			$result = $conn->get_result();
			for ($i = 0; $row = $result->fetch_assoc(); $i++) {
				$count[$i] = new count_entity();
				$id = $row['id'];
				$page = $row['page'];
				$ip = $row['ip'];
				$time = $row['time'];
				$count[$i]->set_id($id);
				$count[$i]->set_page($page);
				$count[$i]->set_ip($ip);
				$count[$i]->set_time($time);

			}

		}
		return $count;

	}

	/*
	 * calc the time
	 * $time :  limit time to add count
	 */
	function count_check($ip, $time) {
		/*
		 * get the lastest record
		 */
		$conn = new conn();

		$sql = sprintf("SELECT TIME_TO_SEC( NOW( ) ) - TIME_TO_SEC( time ) FROM count WHERE ip='%s' ORDER BY `TIME_TO_SEC( NOW( ) ) - TIME_TO_SEC( time )` ASC LIMIT 0 , 1 ", $ip);

		$conn->set_sql($sql);

		$line = $conn->get_line();
		if ($line) {
			$result = $conn->get_result();
			$row = $result->fetch_row();
      if($row[0] < 0)
      $row[0] = 1 ;
			if ($row[0] > $time) {

				return true;
			} else {
				return false;
			}
		} else {
			return true;
		}
	}

	/*
	 * get the counts number by page
	 */
	function counts_get($page) {
		$conn = new conn();

		if (!empty ($page)) {
			$sql = sprintf("select * from count where page='%s'", $page);
		} else {
			$sql = "select * from count";
		}
		$conn->set_sql($sql);
		$line = $conn->get_line();
		return $line;

	}
	/*
	* subdivide the string with substr used for count display
	*/
	function counts_display($count){
		if(!empty($count)){
		$max=strlen($count);
		$c=array();
	for($i=0;$i<$max;){
				$i++;
			$c[$max-$i]=substr($count,-$i,1).'.gif';
			}

			return $c;
		}else{
			return null;
			}
		}
	/*
	* common function
	*/

	function set_url($url) {
		$this->url = $url;
	}

	function get_url() {
		return $this->url;
	}
}
?>